<template>
  <div>
    <!-- <el-row style="margin: 20px 0">
      <el-col :span="7">
        <el-input v-model="key" placeholder="请输入内容"></el-input>
      </el-col>
      <el-col :span="8" style="margin: 0 10px">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator=","
          start-placeholder="Start date"
          end-placeholder="End date"
        >
        </el-date-picker>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" @click="search">搜索</el-button>
      </el-col>
    </el-row>-->
    <el-button type="primary" @click="openModal">添加</el-button>
    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="questionData">
        <el-form-item label="问题标题">
          <el-input
            v-model="questionFormData.problemTitle"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="问题内容">
          <el-input
            v-model="questionFormData.problemText"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="questionData" style="width: 100%">
      <el-table-column
        prop="problemTitle"
        label="问题"
        width="180"
      ></el-table-column>
      <el-table-column prop="problemText" label="问题详情"></el-table-column>
      <el-table-column prop="user.username" label="提问用户"></el-table-column>
      <el-table-column prop="createTime" label="上传时间"></el-table-column>
      <el-table-column width="180" label="操作">
        <template slot-scope="scope">
          <el-button size="mini">编辑</el-button>
          <el-popconfirm
            title="确认删除此条记录"
            @confirm="handleDelete(scope.row)"
          >
            <el-button size="mini" type="danger" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      sytl
      layout="prev, pager, next"
      :total="totalCount"
      :current-page="currentPage"
      :on-success="handleAvatarSuccess"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>
<script>
import dayjs from "dayjs";
export default {
  data() {
    return {
      key: "",
      date: [],
      typeOptions: [],
      typeValue: "",
      questionData: [],
      dialogFormVisible: false,
      questionFormData: {
        problemTitle: "",
        problemText: "",
        createTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
        userId: 1,
      },
      imageUrl: "",
      totalCount: 0, // 总记录数
      pageSize: 10, // 每页显示的记录数
      currentPage: 1, // 当前页码
    };
  },
  methods: {
    openModal() {
      this.dialogFormVisible = !this.dialogFormVisible;
      this.$http.get("http://localhost:8080/articleType/getAll").then((res) => {
        this.typeOptions = res.data.data;
      });
    },
    handleAdd() {
      this.$http
        .post("http://localhost:8080/consult/problem/add", {
          ...this.questionFormData,
        })
        .then((res) => {
          console.log(res);
          this.dialogFormVisible = false;
        });
    },

    handleDelete(row) {
      console.log(row);

      this.$http
        .delete(`http://localhost:8080/consult/problem/delete/${row.pid}`)
        .then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });

          this.$http
            .get(
              `http://localhost:8080/consult/all?pageNum=${this.currentPage}&pageSize=${this.pageSize}`
            )
            .then((res) => {
              this.questionData = res.data.data.list;
              this.totalCount = res.data.data.total;
            });
        });
    },

    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.$http
        .get(
          `http://localhost:8080/consult/all?pageNum=${this.currentPage}&pageSize=${this.pageSize}`
        )
        .then((res) => {
          this.questionData = res.data.data.list;
          this.totalCount = res.data.data.total;
        });
    },
  },

  computed: {
    displayRow() {
      return this.articleTableData.filter((item) => item.status === 1);
    },
  },

  created() {
    this.$http
      .get(
        `http://localhost:8080/consult/all?pageNum=${this.currentPage}&pageSize=${this.pageSize}`
      )
      .then((res) => {
        this.questionData = res.data.data.list;
        this.totalCount = res.data.data.total;
      });
  },
};
</script>

<style>
.img-container {
  width: 70px;
  height: 70px;
  position: relative;
  border-radius: 20%;
}

.img-container > img {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  object-fit: cover;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 20%;
}

.play-icon {
  font-size: 30px;
  line-height: 70px;
  margin: 0 auto;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
